﻿#region License
// MIT License
//
// Copyright(c) 2019-2025 ZhangYu
// https://gitee.com/zhangyu800/litebyte
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#endregion
#region Intro
// Purpose: Reflection Optimize Performance
// Author: ZhangYu
// CreateDate: 2025-02-19
// LastModifiedDate: 2025-02-19
#endregion
namespace LiteByte.Reflection {

    using System.Reflection;

    /// <summary> 属性包装器接口 </summary>
    public interface IPropertyWrapper {

        /// <summary> 获取属性的值 </summary>
        object GetValue(object instance);
        /// <summary> 设置属性的值 </summary>
        void SetValue(object instance, object value);

    }

    /// <summary> 属性反射包装器 </summary>
    public class PropertyReflectionWrapper : IPropertyWrapper {

        private PropertyInfo property;
        private MethodInfo getter;
        private MethodInfo setter;

        public PropertyReflectionWrapper(PropertyInfo propertyInfo) {
            property = propertyInfo;
            getter = property.GetGetMethod();
            setter = property.GetSetMethod();
        }

        public object GetValue(object instance) {
            return getter.Invoke(instance, null);
        }

        public void SetValue(object instance, object value) {
            setter.Invoke(instance, new object[] { value });
        }

    }

    /// <summary> 属性代理包装器 </summary>
    public class PropertyDelegateWrapper<T, V> : IPropertyWrapper {

        private PropertyInfo property;
        private Func<T, V> getter;
        private Action<T, V> setter;

        public PropertyDelegateWrapper(PropertyInfo propertyInfo) {
            property = propertyInfo;
            getter = (Func<T, V>)Delegate.CreateDelegate(typeof(Func<T, V>), property.GetGetMethod());
            setter = (Action<T, V>)Delegate.CreateDelegate(typeof(Action<T, V>), property.GetSetMethod());
        }

        public object GetValue(object instance) {
            return getter((T)instance);
        }

        public void SetValue(object instance, object value) {
            setter((T)instance, (V)value);
        }

    }

}
